package com.wisely.highlight_spring4.ch3.taskexecutor;

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
 * @author GY
 * 2018年1月30日
 * Spring通过任务执行器来实现多线程和并发编程
 * 使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor
 */
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch3.taskexecutor")
//开启对异步任务的支持
@EnableAsync //1 
public class TaskExecutorConfig implements AsyncConfigurer{//2

    //配置类实现AsyncConfigurer接口并且重写getAsyncExecutor方法
    //并且返回一个ThreadPoolTaskExecutor,这样我们就获得了一个基于线程池TaskExecutor
	@Override
	public Executor getAsyncExecutor() {//2
		 ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
	        taskExecutor.setCorePoolSize(5);
	        taskExecutor.setMaxPoolSize(10);
	        taskExecutor.setQueueCapacity(25);
	        taskExecutor.initialize();
	        return taskExecutor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return null;
	}

}
